package com.cj.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cj.entity.SignDayVO;
import com.cj.util.RedisObject;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


@Controller
public class SignController {
	
	@Autowired
	RedisTemplate<String, String> redisTemplate;
	
	@Autowired
	RedisObject redisObject;
	
	@RequestMapping("/sign")
	public String toSign(Model model){
		int year = DateUtil.year(DateUtil.date());
		int month = DateUtil.month(DateUtil.date());
		String key="sign:123:"+year+month;
		long count = redisObject.bitCount(key, redisTemplate);
		JSONObject json1 = JSONUtil.createObj();
		String jsonStr="";
//		JSONArray jsonarray = new JSONArray();
		if(count<=0) {
			return "sign";
		}else {
			byte[] bits = redisObject.getBits(key, redisTemplate);//签到的所有数字十进制
			String binaryStr = NumberUtil.getBinaryStr(bits[0]);
			if(binaryStr.length()>0 && binaryStr!=null) {
				char[] array = binaryStr.toCharArray();
				char[] arr = new char[100];
				arr[0] = '0';
				for (int i = 0,y = 1; i < array.length; i++,y++) {
					arr[y] = array[i];
				}
				for (int i = 0; i < arr.length; i++) {
					int intNum = arr[i] - '0';
					if(i==0) {
						jsonStr="[";
					}
					if(intNum==1) {
						int index = i+1;
						
						String temp = "{\"signDay\":" + "\""+index+"\"},";
						jsonStr = jsonStr + temp;
					}
				}
				jsonStr = jsonStr.substring(0, jsonStr.length()-1);
				jsonStr = jsonStr + "]";
			}
		}
		System.out.println(jsonStr);
		model.addAttribute("redis_object",jsonStr);
		return "sign";
	}
	
	@RequestMapping(value = "/signapi",method = RequestMethod.POST)
	public String signApi(Model model,Long day) {
		int year = DateUtil.year(DateUtil.date());
		int month = DateUtil.month(DateUtil.date());
		String key="sign:123:"+year+month;
		day = day-1;
		redisTemplate.opsForValue().setBit(key,day, true);
		return "sign";
	}
	
	@RequestMapping("/getSign")
	@ResponseBody
	public List<SignDayVO> getSign(Model model){
		List<SignDayVO> voList = new ArrayList<SignDayVO>();
		int year = DateUtil.year(DateUtil.date());
		int month = DateUtil.month(DateUtil.date());
		String key="sign:123:"+year+month;
		long count = redisObject.bitCount(key, redisTemplate);
		if(count<=0) {
			return voList;
		}else {
			byte[] bits = redisObject.getBits(key, redisTemplate);//签到的所有数字十进制
			String binaryStr = NumberUtil.getBinaryStr(bits[0]);
			if(binaryStr.length()>0 && binaryStr!=null) {
				char[] array = binaryStr.toCharArray();
				char[] arr = new char[100];
				arr[0] = '0';
				for (int i = 0,y = 1; i < array.length; i++,y++) {
					arr[y] = array[i];
				}
				for (int i = 0; i < arr.length; i++) {
					int intNum = arr[i] - '0';
					if(intNum==1) {
						int index = i+1;
						SignDayVO vo = new SignDayVO();
						vo.setSignDay(index);
						voList.add(vo);
					}
				}
			}
		}
		System.out.println(voList);
		return voList;
		
	}
	
	
	// 字符串转二进制  
	public  byte[] hex2byte(String str) {   
		  if (str == null){  
		   return null;  
		  }  
		    
		  str = str.trim();  
		  int len = str.length();  
		    
		  if (len == 0 || len % 2 == 1){  
		   return null;  
		  }  
		    
		  byte[] b = new byte[len / 2];  
		  try {  
		       for (int i = 0; i < str.length(); i += 2) {  
		            b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();  
		       }  
		       return b;  
		  } catch (Exception e) {  
		   return null;  
		  }  
		}  
	
	
	

}
